<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<script th:inline="javascript">
    var cityId = '[(${cityId})]';
    var countryId = '[(${countryId})]';
    var isCity = [(${isCity})];
    var isCountry = [(${isCountry})];

    function doGetCityCountry[(${cityId})](data, isForCountry) {
        var arrCity = data.city;
        var arrCountry = data.country;
        if (isCity) {
            if (o(cityId) == null) mui.toast('缺少城市输入框');
            else if (arrCity.length > 0) {
                var c = $('#' + cityId).val();
                let opts = '';
                for (var k in arrCity) {
                    var json = arrCity[k];
                    opts += '<option value="' + json.id + '">' + json.name + '</option>';
                }
                $('#' + cityId).html(opts);
                $('#' + cityId).val(c);
            }
        }
        if (isForCountry) {
            if (o(countryId) == null) mui.toast('缺少区县输入框');
            else {
                var c = $('#' + countryId).val();
                console.log('doGetCityCountry country value', c);
                let opts = '';
                for (var k in arrCountry) {
                    var json = arrCountry[k];
                    opts += '<option value="' + json.id + '">' + json.name + '</option>';
                }
                $('#' + countryId).html(opts);
                $('#' + countryId).val(c);
            }
        }
    }

    function ajaxShowCityCountry(province, city) {
        $.ajax({
            async: false,
            type: "post",
            url: '../../flow/macro/getCityCountry',
            data: {
                rid: cityId,
                cityId: cityId,
                countryId: countryId,
                isCity: isCity,
                isCountry: isCountry,
                province: province,
                city: city
            },
            dataType: "json",
            beforeSend: function(XMLHttpRequest){
            },
            success: function(res, status){
                var isForCountry = province === '';
                doGetCityCountry[(${cityId})](res.data, isForCountry);
            },
            complete: function(XMLHttpRequest, status){
            },
            error: function(XMLHttpRequest, textStatus){
                // 请求出错处理
                alert(XMLHttpRequest.responseText);
            }
        });
    }

    jQuery(function() {
        ajaxShowCityCountry(o('[(${fieldName})]').value, '');
        if (isCountry) {
            ajaxShowCityCountry('', o('[(${cityId})]').value);
        }
    });
</script>
</body>
</html>